local xuying = fk.CreateSkill{
  name = "kl__xuying",
}


xuying:addEffect(fk.DamageInflicted, {
  anim_type = "masochism",
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xuying.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(4)
    local to, card = room:askToChooseCardsAndPlayers(player, {
      min_card_num = 1,
      max_card_num = 1,
      min_num = 0,
      max_num = 1,
      targets = room.alive_players,
      skill_name = xuying.name,
      pattern = tostring(Exppattern{ id = cards }),
      expand_pile = cards,
      prompt = "#kl__xuying-give",
      cancelable = false,
    })
    if #to > 0 then
      room:obtainCard(to[1], card, false, fk.ReasonJustMove, player, xuying.name, {"@@kl__xuying-inhand", 1})
    else
      room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonJustMove, xuying.name, nil, true, player)
    end
    table.removeOne(cards, card[1])
    local result = room:askToGuanxing(player, {
      skill_name = xuying.name,
      cards = cards,
      top_limit = { 2, 6},
      bottom_limit = { 1, 1},
      prompt = "#kl__xuying",
    })
    room:obtainCard(player, result.top[2], false, fk.ReasonJustMove, player, xuying.name)
  end,
})

xuying:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card:getMark("@@kl__xuying-inhand") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.extraUse = true
  end,
})

xuying:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and card:getMark("@@kl__xuying-inhand") > 0
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    return card and card:getMark("@@kl__xuying-inhand") > 0
  end,
})


return xuying
